package models

import (
	"base_srv/basic/config"
	"time"
)

type Platform struct {
	Id              uint32    `gorm:"column:id;type:int UNSIGNED;primaryKey;not null;" json:"id"`
	PlatformName    string    `gorm:"column:platform_name;type:varchar(30);comment:平台名称;not null;" json:"platform_name"`        // 平台名称
	PlatformVersion string    `gorm:"column:platform_version;type:varchar(20);comment:平台版本号;not null;" json:"platform_version"` // 平台版本号
	VersionCode     string    `gorm:"column:version_code;type:varchar(20);not null;" json:"version_code"`
	UserId          uint32    `gorm:"column:user_id;type:int UNSIGNED;not null;" json:"user_id"`
	UploadTime      time.Time `gorm:"column:upload_time;type:datetime;comment:上传时间;not null;" json:"upload_time"`               // 上传时间
	StartUpdateTime time.Time `gorm:"column:start_update_time;type:datetime;comment:开始更新时间;not null;" json:"start_update_time"` // 开始更新时间
	CreateAt        time.Time `gorm:"column:create_at;type:datetime;not null;default:CURRENT_TIMESTAMP;" json:"create_at"`
	UpdateAt        time.Time `gorm:"column:update_at;type:datetime;not null;default:CURRENT_TIMESTAMP;" json:"update_at"`
	DeleteAt        time.Time `gorm:"column:delete_at;type:datetime;default:NULL;" json:"delete_at"`
}

func (p *Platform) TableName() string {
	return "platform"
}

func (p *Platform) Create() error {
	return config.DB.Create(p).Error
}

// Update 修改
func (p *Platform) PlatformUpdate() error {
	return config.DB.Updates(&p).Error
}

// 删除
func (p *Platform) PlatformDel() error {
	return config.DB.Delete(&p).Error
}

// List 列表
func (p *Platform) PlatformList() ([]PlatformListResp, error) {
	var list []PlatformListResp
	err := config.DB.Model(&Platform{}).Select("platform.id, platform.platform_name, platform.platform_version, platform.version_code," +
		"platform.user_id, platform.upload_time, platform.start_update_time,user.real_name").Joins("join user on platform.user_id = user.id").
		Where("platform.delete_at is null").Find(&list).Error
	return list, err
}

type PlatformListResp struct {
	Id              uint32    `json:"id"`
	PlatformName    string    `json:"platformName"`
	PlatformVersion string    `json:"platformVersion"`
	VersionCode     string    `json:"versionCode"`
	RealName        string    `json:"realName"`
	UploadTime      time.Time `json:"uploadTime"`
	StartUpdateTime time.Time `json:"startUpdateTime"`
}
